<template>
  <div style="background-color: #fff; height: 200px" v-show="isSelect">
    <span
      style="
        text-align: center;
        color: dimgray;
        font-size: 20px;
        font-weight: 600;
      "
      >查询条件</span
    >
    <hr style="margin: 10px 0 10px 0" />
    <span
      style="
        text-align: center;
        color: dimgray;
        font-size: 14px;
        font-weight: 600;
      "
      >工作中心编码：</span
    >
    <el-input
      v-model="form.workCenterNum"
      placeholder="请输入工作中心编码"
      style="width: 400px; margin: 10px 0 10px 10px"
    />
    <span
      style="
        text-align: center;
        color: dimgray;
        font-size: 14px;
        font-weight: 600;
        margin: 10px 0 10px 10px;
      "
      >工作中心名称：</span
    >
    <el-input
      v-model="form.workCenterName"
      placeholder="请输入工作中心名称"
      style="width: 400px; margin: 10px 0 10px 10px"
    />
    <br />
    <span
      style="
        text-align: center;
        color: dimgray;
        font-size: 14px;
        font-weight: 600;
      "
      >工作中心类型：</span
    >
    <el-select
      v-model="form.workCenterType"
      style="width: 400px; margin: 10px 0 10px 10px"
    >
      <el-option label="请选择" value="请选择" />
      <el-option label="生产组" value="生产组" />
      <el-option label="加工组" value="加工组" />
    </el-select>
    <span
      style="
        text-align: center;
        color: dimgray;
        font-size: 14px;
        font-weight: 600;
        margin: 10px 0 10px 10px;
      "
      >状态：</span
    >
    <el-select
      v-model="form.abc"
      style="width: 400px; margin: 10px 0 10px 10px"
    >
      <el-option label="请选择" :value="0" />
      <el-option label="有效" :value="1" />
      <el-option label="无效" :value="2" />
    </el-select>
    <br />
    <el-button
      style="float: right; margin: 2px 0 10px 10px; width: 100px; height: 35px"
      @click="resetForm"
      >重置</el-button
    >
    <el-button
      type="primary"
      style="float: right; margin: 2px 0 10px 0; width: 100px; height: 35px"
      :icon="Search"
      @click="LoadShowData"
      >查询</el-button
    >
  </div>

  <div style="background-color: #fff; height: 500px; margin-top: 20px">
    <span
      style="
        text-align: center;
        color: dimgray;
        font-size: 20px;
        font-weight: 600;
      "
      >工作中心列表</span
    >

    <el-button
      style="float: right; margin: 2px 0 10px 10px"
      :icon="Search"
      @click="
        (isSelect = !isSelect), (Select = isSelect ? '收起查询' : '展开查询')
      "
      >{{ Select }}</el-button
    >
    <el-button
      type="primary"
      style="float: right; margin: 2px 0 10px 10px"
      :icon="Edit"
      @click="router.push('/WorkCenters/WorkCenterAddHtml')"
      >新增工作中心</el-button
    >
    <el-button
      type="info"
      style="float: right; margin: 2px 0 10px 10px"
      :icon="Search"
      @click="exportShowData()"
      >导出</el-button
    >
    <el-upload
      class="upload-demo"
      :action="apiurlUpd + '/api/WorkCenter/GetImport'"
      multiple
      :limit="3"
      style="float: right; margin-top: 0px; margin-right: 20px"
    >
      <el-button type="primary">导入</el-button>
    </el-upload>
    <hr style="margin: 15px 0 10px 0" />
    <el-table :data="WorkCenterData" style="width: 100%">
      <el-table-column prop="workCenterId" label="序号" />
      <el-table-column prop="workCenterNum" label="工作新增编码" />
      <el-table-column prop="workCenterName" label="工作新增名称" />
      <el-table-column prop="workCenterType" label="工作新增类型" />
      <el-table-column prop="classesData" label="班次" />
      <el-table-column prop="createTime" label="创建时间">
        <template #default="{ row }">
          {{ Moment(row.createTime).format("YYYY-MM-DD HH:mm:ss") }}
        </template>
      </el-table-column>
      <el-table-column prop="createName" label="创建人" />
      <el-table-column label="状态">
        <template #default="{ row }">
          <el-tag type="success" v-if="row.workCenterState == true">
            {{ row.workCenterState == true ? "有效" : "无效" }}
          </el-tag>
          <el-tag type="info" v-else>
            {{ row.workCenterState == true ? "有效" : "无效" }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="approvalState" label="审批状态">
        <template #default="{ row }">
          <el-tag type="info" v-if="row.approvalState == 0">
            {{
              row.approvalState == 0
                ? "未审批"
                : row.approvalState == 1
                ? "通过"
                : "驳回"
            }}
          </el-tag>
          <el-tag type="success" v-else-if="row.approvalState == 1">
            {{
              row.approvalState == 0
                ? "未审批"
                : row.approvalState == 1
                ? "通过"
                : "驳回"
            }}
          </el-tag>
          <el-tag type="danger" v-else>
            {{
              row.approvalState == 0
                ? "未审批"
                : row.approvalState == 1
                ? "通过"
                : "驳回"
            }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="350px">
        <template #default="{ row }">
          <el-button type="danger" @click="WorkCenterDelete(row)"
            >删除</el-button
          >
          <el-button
            v-show="row.approvalState == 0 ? true : false"
            type="success"
            @click="WorkCenterUpdate(row)"
            >修改</el-button
          >
          <!-- 通过 -->
          <el-button
            v-show="row.approvalState == 0 ? true : false"
            type="success"
            @click="tongguo(row, 1)"
            >通过</el-button
          >
          <!-- 驳回 -->
          <el-button
            v-show="row.approvalState == 0 ? true : false"
            type="warning"
            @click="bohui(row, 2)"
            >驳回</el-button
          >
          <el-button
            v-show="row.approvalState == 2 ? true : false"
            type="warning"
            @click="bohuifaqi(row, 0)"
            >驳回发起</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div>
      <el-pagination
        v-model:current-page="form.pageindex"
        v-model:page-size="form.pagesize"
        layout="prev, pager, next"
        :total="form.totalcount"
        class="mt-4"
        @current-change="handleCurrentChange"
        style="float: right"
      />
      <span style="float: right; margin-top: 4px"
        >共{{ form.totalcount }}条</span
      >
    </div>
  </div>
  <!-- 分页 -->
</template>

<script setup lang="ts">
import Moment from "moment";
import { useRouter } from "vue-router";
const router = useRouter();
import { Delete, Edit, Search, Share, Upload } from "@element-plus/icons-vue";
import axios from "axios";
import { ref, reactive, onMounted } from "vue";
import { ElMessage } from "element-plus";
const isSelect = ref(false);
const Select = ref("展开查询");
const apiurl = import.meta.env.VITE_APP_API_URL_ProductionShow;
const apiurlUpd = import.meta.env.VITE_APP_API_URL_ProductionAdd;
//获取登录信息
const UserList = ref([]);

const workflowIda: any = ref("");
//审批需要编号获取
const LoadUserJSONData = () => {
  //读出数据
  const value = localStorage.getItem("userInfo");
  //反序列化JSON字符串
  UserList.value = JSON.parse(value);
  UserList.value.forEach((item: any) => {
    name.value = item.userName;
  });
  workflowIda.value = localStorage.getItem("workflowIda");
  console.log(workflowIda.value);
};
const exportShowData = () => {
  location.href = `${apiurl}/api/W/WorkCenterExportExcel`;
};

//查询条件1
const form = reactive({
  pageindex: 1,
  pagesize: 3,
  workCenterNum: "",
  workCenterName: "",
  workCenterType: "请选择",
  workCenterState: true,
  abc: 0,
  totalcount: 0,
  pagecount: 0,
});

//查询条件重置
const resetForm = () => {
  form.workCenterNum = "";
  form.workCenterName = "";
  form.workCenterType = "";
  form.workCenterState = true;
  form.abc = 0;
};
//页面初始化数据
const WorkCenterData = ref([]);
const LoadShowData = () => {
  if (form.abc == 1) {
    form.workCenterState = true;
  } else if (form.abc == 2) {
    form.workCenterState = false;
  } else {
  }
  //http://localhost:5195
  axios.post(apiurl + "/api/W/WorkCenterShowData", form).then((res: any) => {
    console.log(res.data.data);
    WorkCenterData.value = res.data.data.list;
    form.totalcount = res.data.data.totalcount;
    form.pagecount = res.data.data.pagecount;
  });
};
//分页
const handleCurrentChange = (val: number) => {
  form.pageindex = val;
  LoadShowData();
};
const name = ref("");
//删除
const WorkCenterDelete = (row: any) => {
  console.log(row);
  const data = {
    workCenterId: row.workCenterId,
    name: name.value,
  };
  axios
    .post(apiurlUpd + "/api/WorkCenter/WorkCenterDeleteData", data)
    .then((res: any) => {
      console.log(res.data);
      if (res.data.code > 0) {
        ElMessage.success(res.data.msg);
      } else {
        ElMessage.error(res.data.msg);
      }
      LoadShowData();
    });
};

//审批
const userId = ref(0);
const aaa = import.meta.env.VITE_APP_API_URL_StartWorkflowData;
const tongguo = (res: any, num: number) => {
  if (name.value == "MaxAdmin") {
    userId.value = 1;
    name.value = "MaxAdmin";
  } else if (name.value == "Admin") {
    userId.value = 2;
    name.value = "admin";
  } else if (name.value == "李四") {
    userId.value = 4;
    name.value = "李四";
  } else {
    userId.value = 0;
    name.value = "";
  }
  //查询编号信息
  const dataa = {
    workCenterNum: res.workCenterNum,
    Title: "工作中心任务",
  };
  axios
    .post(apiurl + "/api/W/WorkflowInstanceShowCommand", dataa)
    .then((res: any) => {
      if (res.data.code > 0) {
        //存入数据
        localStorage.setItem(
          "workflowIdaa",
          res.data.data[0].workflowInstanceId
        );
      }
    });
  console.log(localStorage.getItem("workflowIdaa"));
  //开始审批
  const data = {
    workflowId: localStorage.getItem("workflowIdaa"),
    auditOperateType: 3,
    userId: userId.value,
    userName: name.value,
    rejectToStepId: "",
    remark: "通过",
  };
  console.log(data, "开始审批");
  axios.post(aaa + "/api/Workflow/UpAuditData", data).then((res: any) => {
    console.log(res.data);
    if (res.data.code > 0) {
      ElMessage.success(res.data.msg);
      //删除数据
      localStorage.removeItem("workflowIdaa");
    } else {
      ElMessage.error(res.data.msg);
    }
    LoadShowData();
  });

  //可不做  （工作中心表的状态修改）
  const adata = {
    workCenterId: res.workCenterId,
    name: name.value,
    num: num,
  };
  axios
    .post(apiurlUpd + "/api/WorkCenter/WorkCenterUpdApproveData", adata)
    .then((res: any) => {
      console.log(res.data);
      if (res.data.code > 0) {
        ElMessage.success(res.data.msg);
      } else {
        ElMessage.error(res.data.msg);
      }
      LoadShowData();
    });
};
//驳回
const bohui = (res: any, num: number) => {
  if (name.value == "MaxAdmin") {
    userId.value = 1;
    name.value = "MaxAdmin";
  } else if (name.value == "Admin") {
    userId.value = 2;
    name.value = "admin";
  } else if (name.value == "李四") {
    userId.value = 4;
    name.value = "李四";
  } else {
    userId.value = 0;
    name.value = "";
  }
  const dataa = {
    workCenterNum: res.workCenterNum,
    Title: "工作中心任务",
  };
  axios
    .post(apiurl + "/api/W/WorkflowInstanceShowCommand", dataa)
    .then((res: any) => {
      if (res.data.code > 0) {
        console.log(res.data);
        localStorage.setItem(
          "workflowIdaa",
          res.data.data[0].workflowInstanceId
        );
      }
    });
  console.log(localStorage.getItem("workflowIdaa"));
  //在数据从本地取出后  格式为  "123" 的格式 在添加到下面data时 需要去掉  ""
  //workflowId.value = workflowId.value.substring(1, workflowId.value.length - 1);
  const data = {
    workflowId: localStorage.getItem("workflowIdaa"),
    auditOperateType: 2,
    userId: userId.value,
    userName: name.value,
    rejectToStepId: "",
    remark: "驳回",
  };
  console.log(data, "开始驳回");
  axios.post(aaa + "/api/Workflow/UpAuditData", data).then((res: any) => {
    console.log(res.data);
    if (res.data.code > 0) {
      ElMessage.success(res.data.msg);
      //删除数据
      localStorage.removeItem("workflowIdaa");
    } else {
      ElMessage.error(res.data.msg);
    }
  });
  //可不做    （工作中心表的状态修改）
  const adata = {
    workCenterId: res.workCenterId,
    name: name.value,
    num: num,
  };
  axios
    .post(apiurlUpd + "/api/WorkCenter/WorkCenterUpdApproveData", adata)
    .then((res: any) => {
      console.log(res.data);
      if (res.data.code > 0) {
        ElMessage.success(res.data.msg);
      } else {
        ElMessage.error(res.data.msg);
      }
      LoadShowData();
    });
};
//驳回发起
const bohuifaqi = (res: any, num: number) => {
  if (name.value == "MaxAdmin") {
    userId.value = 1;
    name.value = "MaxAdmin";
  } else if (name.value == "Admin") {
    userId.value = 2;
    name.value = "admin";
  } else if (name.value == "李四") {
    userId.value = 4;
    name.value = "李四";
  } else {
    userId.value = 0;
    name.value = "";
  }
  const dataa = {
    workCenterNum: res.workCenterNum,
    Title: "工作中心任务",
  };
  axios
    .post(apiurl + "/api/W/WorkflowInstanceShowCommand", dataa)
    .then((res: any) => {
      if (res.data.code > 0) {
        console.log(res.data);
        localStorage.setItem(
          "workflowIdaa",
          res.data.data[0].workflowInstanceId
        );
      }
    });
  const data = {
    workflowId: localStorage.getItem("workflowIdaa"),
    auditOperateType: 0,
    userId: userId.value,
    userName: name.value,
    rejectToStepId: "",
    remark: "驳回发起",
  };
  axios.post(aaa + "/api/Workflow/UpDateRerectData", data).then((res: any) => {
    console.log(res.data);
    if (res.data.code > 0) {
      ElMessage.success(res.data.msg);
      //删除数据
      localStorage.removeItem("workflowIdaa");
    } else {
      ElMessage.error(res.data.msg); 
    }
  });
  //可不做    （工作中心表的状态修改）
  const adata = {
    workCenterId: res.workCenterId,
    name: name.value,
    num: num,
  };
  axios
    .post(apiurlUpd + "/api/WorkCenter/WorkCenterUpdApproveData", adata)
    .then((res: any) => {
      console.log(res.data);
      if (res.data.code > 0) {
        ElMessage.success(res.data.msg);
      } else {
        ElMessage.error(res.data.msg);
      }
      LoadShowData();
    });
};

//修改跳转页面
const WorkCenterUpdate = (row: any) => {
  router.push({
    path: "/WorkCenters/WorkCenterAddHtml",
    query: {
      workCenterId: row.workCenterId,
    },
  });
};

//生命周期钩子函数
onMounted(() => {
  LoadShowData();
  LoadUserJSONData();
});
</script>

<style scoped></style>
